﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortingNetworkSimulator.Model
{
    public class SortingTester
    {
        private Network network;

        public SortingTester(Network network)
        {
            this.network = network;
        }

        public bool IsSortingNetwork()
        {
            ulong permutationCount = 1ul << network.InputsCount;

            for (ulong i = 0; i < permutationCount; i++)
            {
                string binaryString = Convert.ToString((long)i, 2);
                double[] input = binaryString.PadLeft(network.InputsCount, '0')
                    .Select(b => Char.GetNumericValue(b)).ToArray();

                network.Process(input);

                if (!IsSorted(input))
                {
                    return false;
                }
            }

            return true;
        }

        public bool IsSorted(double[] input)
        {
            for (int i = 1; i < input.Length; i++)
            {
                if (input[i - 1] > input[i])
                {
                    return false;
                }
            }

            return true;
        }
    }
}
